package cn.cimoc.handler;
import cn.cimoc.model.vo.ResultVO;
import cn.cimoc.constant.ErrorEnum;
import cn.cimoc.util.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author  LGZ
 * @date 2021/5/14 21:13
 * <p>
 * 当前端数据传入的json格式错误，而后端需要的是json时，springboot会抛出{@link HttpMessageNotReadableException}
 * 捕获这个异常并返回错误提示
 */

@ControllerAdvice
public class HttpMessageNotReadableExceptionHandler {
    private  final Logger log = LoggerFactory.getLogger(HttpMessageNotReadableExceptionHandler.class);

    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    @ResponseBody
    public ResultVO error(HttpMessageNotReadableException e) {
        log.info("前端传入数据异常");
        return CommonUtils.ajaxReturn(ErrorEnum.BAD_REQUEST);
    }

}
